import java.util.ArrayList;
import java.util.Arrays;

public class Test {

    public static void countSort(int[] array) {
        //找到最大值和最小值然后开辟的数组
        int min = array[0];
        int max = array[0];
        for (int i = 1; i < array.length; i++) {
            if(array[i] < min) {
                min = array[i];
            }
            if(array[i] > max) {
                max = array[i];
            }
        }
        int[] count = new int[max - min + 1];

        //遍历原数组，制作计数数组
        for (int i = 0; i < array.length; i++) {
            count[array[i] - min]++;
        }

        int k = 0;
        //利用计数数组给原数组进行计数排序
        for (int i = 0; i < count.length; i++) {
            while(count[i] != 0) {
                array[k++] = i + min;
                count[i]--;
            }
        }
    }

    public static void main(String[] args) {
        int[] arr = {14,16,12,10,14,12,18,19,20,15,16,12,11,11};
        countSort(arr);
        System.out.println(Arrays.toString(arr));
    }
    public static void mergeSortNor(int[] array) {
        int gap = 1;
        while(gap < array.length) {
            for (int i = 0; i < array.length;  i += gap * 2) {
                int start = i;
                int mid = i+gap-1;
                if(mid >= array.length) {
                    mid = array.length-1;
                }
                int end = mid+gap;
                if(end >= array.length) {
                    end = array.length-1;
                }
                merge_sort(array,start,mid,end);
            }
            gap *= 2;
        }
    }


    private static void merge_sort(int[] array, int start, int mid, int end) {
        int[] tmp = new int[end - start +1];
        int k = 0;
        int s1 = start;
        int s2 = mid+1;
        int e1 = mid;
        int e2 = end;

        while(s1 <= e1 && s2 <= e2) {
            if(array[s1] <= array[s2]) {
                tmp[k++] = array[s1++];
            } else {
                tmp[k++] = array[s2++];
            }
        }

        while(s1 <= e1) {
            tmp[k++] = array[s1++];
        }

        while(s2 <= e2) {
            tmp[k++] = array[s2++];
        }

        //放回原数组
        for (int i = 0; i < k; i++) {
            array[i+start] = tmp[i];
        }
    }


    private static void merge(int[] array, int start, int end) {
        if(start == end) {
            return;
        }

        int mid = (start + end) / 2;

        //分解
        merge(array,start,mid);
        merge(array,mid+1,end);

        //合并
        merge_sort(array,start,mid,end);
    }
/*
    public static void quickSort(int[] array) {
        quickNor(array,0,array.length - 1);
    }

    private static void quickNor(int[] array, int left, int right) {
        Queue<Integer> queue = new LinkedList<>();

        int pivot = partition(array,left,right);
        if(left < pivot-1) {
            queue.offer(left);
            queue.offer(pivot-1);
        }

        if(right > pivot+1) {
            queue.offer(pivot + 1);
            queue.offer(right);
        }

        while(!queue.isEmpty()) {
            int start = queue.poll();
            int end = queue.poll();
            pivot = partition(array,start,end);

            if(start < pivot-1) {
                queue.offer(start);
                queue.offer(pivot-1);
            }

            if(end > pivot+1) {
                queue.offer(pivot + 1);
                queue.offer(end);
            }
        }
    }

    private static int partition(int[] array, int left, int right) {
        int key = array[left];
        while(left < right) {
            while (left < right && array[right] >= key) {
                right--;
            }
            array[left] = array[right];
            while (left < right && array[left] <= key) {
                left++;
            }
            array[right] = array[left];
        }
        array[left] = key;
        return left;
    }
*/

/*    private static void quick(int[] array, int left, int right) {
        if(right - left <= 20) {
            insertSort(array,left,right);
            return;
        }

        int pivot = partition(array,left,right);

        quick(array,left,pivot-1);
        quick(array,pivot+1,right);
    }

    private static void insertSort(int[] array, int left, int right) {
        for (int i = left + 1; i <= right; i++) {
            int key = array[i];
            int j = i-1;
            for (; j >= 0; j--) {
                if(array[j] > key) {
                    array[j+1] = array[j];
                } else {
                    array[j+1] = key;
                    break;
                }
            }
            array[j+1] = key;
        }
    }

    private static int partition(int[] array, int left, int right) {
        //三数取中法
        getMiddleIndex(array,left,right);

        int key = array[left];
        while(left < right) {
            while(left < right && array[right] >= key) {
                right--;
            }
            array[left] = array[right];

            while(left < right && array[left] <= key) {
                left++;
            }
            array[right] = array[left];
        }

        array[left] = key;
        return left;
    }

    private static void getMiddleIndex(int[] array, int left, int right) {
        int leftValue = array[left];
        int rightValue = array[right];
        int middleIndex = (left + right) / 2;
        int middleValue = array[middleIndex];

        if(leftValue < rightValue) {
            if(middleValue > rightValue) {
                swap(array,left,right);
            } else if(middleValue < leftValue) {
                return;
            } else {
                swap(array,left,middleIndex);
            }
        } else {
            if(middleValue < rightValue) {
                swap(array,left,right);
            } else if(middleValue > leftValue) {
                return;
            } else {
                swap(array,left,middleIndex);
            }
        }
    }

    private static void swap(int[] array, int i, int j) {
        int tmp = array[i];
        array[i] = array[j];
        array[j] = tmp;
    }*/

/*    private static void quick(int[] array, int left, int right) {
        if(left >= right) {
            return;
        }

        int pivot = partition(array,left,right);

        quick(array,left,pivot-1);
        quick(array,pivot+1,right);
    }




  */

   /* public static void quick1(int[] array,int left,int right) {
        if(left >= right) {
            return;
        }

        int pivot = partition(array,left,right);

        quick1(array,left,pivot-1);
        quick1(array,pivot+1,right);
    }

    /*    private static int partition(int[] array, int left, int right) {
        int prev = left;
        int cur = left + 1;
        int key = array[left];

        while(cur <= right) {
            if(array[cur] < key) {
                prev++;
                if(array[prev] != array[cur]) {
                    swap(array,prev,cur);
                }
            }
            cur++;
        }

        swap(array,prev,left);
        return prev;
    }*/

 /*   private static int partition2(int[] array, int left, int right) {
        int key = array[left];
        while(left < right) {
            while(left < right && array[right] >= key) {
                right--;
            }
            array[left] = array[right];
            while(left < right && array[left] <= key) {
                left++;
            }
            array[right] = array[left];
        }

        array[left] = key;
        return left;
    }
*/
/*    public static void main(String[] args) {
        int[] arr = {45,85,23,16,85,42,2,0,36,40};
        mergeSortNor(arr);
        System.out.println(Arrays.toString(arr));
    }*/


}
